home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / comm / tcp / TcpSpeed.lha / TcpSpeed.c next >
Encoding:
C/C++ Source or Header  |  2000-09-06  |  3.4 KB  |  150 lines

  1. #ifdef AMIGA
  2. #include <proto/socket.h>
  3. #endif
  4. #ifdef WIN32
  5. #include <winsock2.h>
  6. #else
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <netdb.h>
  11. #endif
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <time.h>
  17. #if !defined(AMIGA) && !defined(WIN32)
  18. #include <sys/times.h>
  19. #endif
  20.  
  21. /* $$$ comment out the following line if your OS uses old-style
  22.        (BSD 4.2) sockaddr structures
  23.    $$$ */
  24. #ifndef WIN32
  25. #define HASSINLEN
  26. #endif
  27.  
  28. #define PORTNR 37215
  29. #define BUFLEN 32768
  30. #define BUFNUM 640
  31.  
  32. unsigned char buf[BUFLEN];
  33.  
  34. static void usage(char *pname) {
  35.     fprintf(stderr,"Usage: \"%s s\" / \"%s c hostname\"\n",pname,pname);
  36.     exit(0);
  37. }
  38.  
  39. clock_t getelapsed(void) {
  40. #if defined(AMIGA) || defined(WIN32)
  41.     return clock();
  42. #else
  43.     struct tms tms;
  44.     return times(&tms);
  45. #endif
  46. }
  47.  
  48. void main(int argc,char *argv[]) {
  49.     char mode;
  50.     int sock;
  51.     int len,totallen;
  52.     struct sockaddr_in sin;
  53. #ifdef WIN32
  54.     WORD            VersionRequested = MAKEWORD( 2, 0 );
  55.    WSADATA         WsaData;
  56.  
  57.    WSAStartup( VersionRequested, &WsaData );
  58. #endif
  59.  
  60.     if((argc<2)||(argc>3))
  61.         usage(argv[0]);
  62.     mode=tolower(argv[1][0]);
  63.     if((mode!='c')&&(mode!='s'))
  64.         usage(argv[0]);
  65.     if(((mode=='c')&&(argc!=3))||((mode=='s')&&(argc!=2)))
  66.         usage(argv[0]);
  67.     if(0>(sock=socket(AF_INET,SOCK_STREAM,0))) {
  68.         fprintf(stderr,"Unable to create socket\n");
  69.         exit(0);
  70.     }
  71.     if(mode=='c') {
  72.         struct hostent *he;
  73.         clock_t starttime,endtime;
  74.  
  75.         if(!(he=gethostbyname(argv[2]))) {
  76.             fprintf(stderr,"Host not found\n");
  77.             exit(0);
  78.         }
  79.         sin.sin_family=AF_INET;
  80. #ifdef HASSINLEN
  81.         sin.sin_len=sizeof(sin),
  82. #endif
  83.         memcpy(&sin.sin_addr,he->h_addr,he->h_length);
  84.         sin.sin_port=htons(PORTNR);
  85.         if(0>connect(sock,(struct sockaddr *)&sin,sizeof(sin))) {
  86.             fprintf(stderr,"Unable to connect\n");
  87.             exit(0);
  88.         }
  89.         totallen=0;
  90.         starttime=getelapsed();
  91.         while(0<(len=recv(sock,buf,BUFLEN,0)))
  92.             totallen+=len;
  93.         endtime=getelapsed();
  94.  
  95. #ifdef AMIGA 
  96.         CloseSocket(sock);
  97. #elif defined(WIN32)
  98.         closesocket(sock);
  99. #else
  100.         close(sock);
  101. #endif
  102.         printf("%ld bytes read, %d kB/s\n",
  103.          totallen,(((totallen/1024)*CLOCKS_PER_SEC)/(endtime-starttime)));
  104.     } else {
  105.         int sock2,i;
  106.         long on=1;
  107.  
  108.         for(i=0;i<BUFLEN;i++)
  109.             buf[i]=i;
  110. #ifdef SO_REUSEADDR
  111.         setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&on,
  112.          sizeof(on));
  113. #endif
  114.         memset(&sin,0,sizeof(sin));
  115. #ifdef HASSINLEN
  116.         sin.sin_len=sizeof(sin);
  117. #endif
  118.         sin.sin_family=AF_INET;
  119.         sin.sin_addr.s_addr=htonl(INADDR_ANY);
  120.         sin.sin_port=htons(PORTNR);
  121.         if(0>bind(sock,(struct sockaddr *)&sin,sizeof(sin))) {
  122.             fprintf(stderr,"Unable to bind socket\n");
  123.             exit(0);
  124.         }
  125.         listen(sock,5);
  126.         if(0>(sock2=accept(sock,0,0))) {
  127.             fprintf(stderr,"Unable to accept\n");
  128.             exit(0);
  129.         }
  130.         for(i=0;i<BUFNUM;i++)
  131.             if(0>=send(sock2,buf,BUFLEN,0))
  132.                 break;
  133. #ifdef AMIGA
  134.         CloseSocket(sock2);
  135.         CloseSocket(sock);
  136. #elif defined(WIN32)
  137.         closesocket(sock2);
  138.         closesocket(sock);
  139. #else
  140.         close(sock2);
  141.         close(sock);
  142. #endif
  143.     }
  144.  
  145. #ifdef WIN32
  146.     WSACleanup();
  147. #endif
  148. }
  149.  
  150.